<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Users extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('users_mdl');
    }

    public function index(){
        if($_SESSION['user']['group'] != 1){
            show_404();
        }

        //获取所有数据
        $this->db->order_by('group desc');
        $this->db->where('status !=',3);
        $this->db->order_by('status asc , group desc');
        $list = $this->users_mdl->select();

        $data = array('list'=>$list);
        $this->_view('index',$data);
    }

    /**
     * 用户新增
     */
    public function create_user(){
        if($_SESSION['user']['group'] != 1){
            show_404();
        }

        $this->_view('create');
    }

 /**
     * 执行用户新增
     */
    public function do_create(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！ ');
        }
       $post = $this->input->post();
            if(empty($post['name'])){
                die('姓名不能为空');
            }

            if(empty($post['sex'])){
                die('性别不能为空');
            }

            if(!preg_match("/^1[0-9]{10}$/",$post['phone'])){
                die('联系电话格式错误，请填写11位的手机号码');
            }

            $this->db->where('phone',$post['phone']);
            $rs = $this->users_mdl->get_count();
            if($rs>0){
                die('联系电话已存在，请重新填写');
            }

            if(!preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/",$post['email'])){
                die('联系邮箱格式错误，请填写正确的邮箱地址，如 zbzmall@163.com');
            }
            $this->db->where('email',$post['email']);
            $rs1 = $this->users_mdl->get_count();
            if($rs1>0){
                die('联系邮箱已存在，请重新填写');
            }

            if($post['password']!=$post['repass']){
                die('两次密码不一致，请重新确认');
            }
        $data = array(
            'name' => $post['name'],
            'sex' => $post['sex'],
            'birthday' => strtotime($post['birthday']),
            'phone' => $post['phone'],
            'email' => $post['email'],
            'group' => $post['group'],
            'password' => md5($post['password']),
            'create_time' => time()
            );
            $re = $this->users_mdl->create($data);
            if($re){
                echo 1;
            }

    }

    /**
     * 用户资料修改页面
     */
    public function edit(){
        $id = $this->input->get('id');
        $this->db->where('id',$id);
        $re = $this->users_mdl->get_one();
        $this->_view('edit',$re);
    }

    /**
     * 个人设置修改页面
     */
    public function set(){
       $id = $_SESSION['user']['id'];
       //$id = $this->input->get('id');
       $this->db->where('id',$id);
       $re = $this->users_mdl->get_one();
        $this->_view('set',$re);
    }

    /**
     * 个人设置执行修改
     */
    public function do_set(){
          if(!$this->input->is_ajax_request()){
            show_error('参数错误！ ');
        }
        $post = $this->input->post();
        $data = array(
            'name' => $post['name'],
            'sex' => $post['sex'],
            'phone' => $post['phone'],
            'email' => $post['email'],
            'group' => $post['group'],
            'birthday' => strtotime($post['birthday']),
        );
        $this->db->where('id',$post['id']);
        $re = $this->users_mdl->update($data);
        if($re){
            echo 1;
        }
    }

    /**
     * 用户资料执行修改
     */
    public function do_edit(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！ ');
        }
        $post = $this->input->post();
        $data = array(
            'name' => $post['name'],
            'sex' => $post['sex'],
            'phone' => $post['phone'],
            'email' => $post['email'],
            'group' => $post['group'],
            'birthday' => strtotime($post['birthday']),
        );
        $this->db->where('id',$post['id']);
        $re = $this->users_mdl->update($data);
        if($re){
            echo 1;
        }
    }

    /**
     * 用户删除
     */
    public function del(){
        $id = $this->input->get('id');
        $data = array('status'=>3);
        $this->db->where('id',$id);
        $re = $this->users_mdl->update($data);
         if($re){
            echo 1;
        }
    }

    /**
     * 用户退出
     */
    public function quit(){
        if(!$this->input->is_ajax_request()){
            show_404();
        }

        $this->session->unset_userdata('user');
        echo 1;
    }

    /**
     * 用户密码修改页面
     */
    public function change(){
       $id = $_SESSION['user']['id'];
       $this->db->where('id',$id);
       $re = $this->users_mdl->get_one();
       $this->_view('change',$re);
    }

     /**
     * 用户密码执行修改
     */
    public function do_change(){
       $id = $_SESSION['user']['id'];
       $post = $this->input->post();
       $this->db->where('id',$id);
       $re = $this->users_mdl->get_one();
       if($re['password']!=md5($post['password'])){
        die('原始密码错误,请重新填写');
       }
       if($post['password1']!=$post['password2']){
        die('两次密码不一致，请重新确认');
       }
       $data = array(
            'password' => md5($post['password1']),
        );
       //var_dump($data);exit;
       $this->db->where('id',$id);
       $rs = $this->users_mdl->update($data);
       if($rs){
            session_unset();
            session_destroy();
            echo 1;
       }

       //exit;
    }
}

/* End of file Users.php */
/* Location: ./application/controllers/Users.php */